package pers.zw.study.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcDemo1 {

    public static void main(String[] args) throws Exception {
        //设置数据库、用户名、密码
        String url = "jdbc:mysql://localhost:3306/test";
        String userName = "root";
        String userPwd = "123";
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection con = DriverManager.getConnection(url, userName, userPwd);
        //获取向数据库发送sql语句的statement
        Statement st = con.createStatement();

        String sql = "select * from student";
        ResultSet res = st.executeQuery(sql);

        while (res.next()) {
            System.out.println("s_no = " + res.getObject("s_no"));
            System.out.println("s_name = " + res.getObject("s_name"));
            System.out.println("s_gender = " + res.getObject("s_gender"));
            System.out.println("s_birthday = " + res.getObject("s_no"));
            System.out.println("s_class = " + res.getObject("s_class"));
        }
        //关闭连接释放资源
        res.close();
        st.close();
        con.close();
    }
}
